'use strict';
var HttpGetMethod = "GET";
var HttpPostMethod = "POST";
var HttpPutMethod = "PUT";
var HttpDeleteMethod = "DELETE";
var HttpStatusOk = 200;
var DefaultContentType = "application/x-www-form-urlencoded";
var JSONContentType = "application/json";
var RequestTimeout = 10000; //10 seconds
/**
*Ajax Reqeust Object class
*auth:lww
*version:1.0
*create date:2014-10-11
*/
function AjaxObject(context) {      
    this.version = "1.0.0";
    this.mContext = context;   
    this.maxLoadRepeat = 3;
    this.loadRepeat = 0;
    this.loadUrl = null;
    this.selfHeader = null;
    this.contentType = DefaultContentType;  
    this.XmlHttp = this.GetXmlHttpObject();
}
//set AjaxObject prototype
AjaxObject.prototype.GetXmlHttpObject = function () {
    var xmlhttp = false;
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {//IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();           
        } catch (e) {
            xmlhttp = false;
        }
    }
    else if (!xmlhttp) {
        try {//IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
};
AjaxObject.prototype.DoRequest = function (ActionType, requestUrl, theData, theDataType) {
    if (this.XmlHttp) {
        if (this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0) {
            var oThis = this;
            this.loadUrl = requestUrl;
            this.XmlHttp.open(ActionType, requestUrl, true);
            this.XmlHttp.onreadystatechange = function () { oThis.ReadyStateChange(); };
            if(theDataType){
                this.XmlHttp.setRequestHeader('Content-Type', theDataType);
            }else{
                this.XmlHttp.setRequestHeader('Content-Type', oThis.contentType);
            }            
            this.XmlHttp.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XHR');
            if(this.selfHeader){
                var xmlhttp = this.XmlHttp;
                this.selfHeader.forEach(function(item){xmlhttp.setRequestHeader(item.getName(), item.getValue());});                     
            }
            this.XmlHttp.onprogress = function(pe){ oThis.OnProgress(pe,oThis.mContext); };
            this.XmlHttp.ontimeout = function(e){ oThis.OnTimeout(e,oThis.mContext); };
            this.XmlHttp.timeout = RequestTimeout;           
            if(theData){
                this.XmlHttp.send(theData);
            }          
            else {
                this.XmlHttp.send(null);
            }
            /**repeat count**/
            this.loadRepeat++;
        }
    }
};
AjaxObject.prototype.AbortCallBack = function () {
    if (this.XmlHttp) {
        this.XmlHttp.abort();
    }
};
AjaxObject.prototype.Release = function () {
    if (this.XmlHttp) {
        this.XmlHttp.destroy();
        this.XmlHttp = null;
    }
};
AjaxObject.prototype.OnLoading = function (context) {console.log("AjaxObject loading:");};
AjaxObject.prototype.OnLoaded = function (context) {console.log("AjaxObject loaded:");};
AjaxObject.prototype.OnInteractive = function (context) {console.log("AjaxObject interactive:");};
AjaxObject.prototype.OnTimeout = function (e,context) {console.log("AjaxObject timeout:");};
AjaxObject.prototype.OnProgress = function(pe,context){
    if(pe.lengthComputable) {
        console.log("AjaxObject progress:".concat(pe.loaded).concat("/").concat(pe.total));
    }
};
AjaxObject.prototype.OnComplete = function (responseText,context) {console.log("AjaxObject complete:".concat(responseText));};
AjaxObject.prototype.OnXmlComplete = function (responseXml,context) {console.log("AjaxObject xml complete:".concat(responseXml));};
AjaxObject.prototype.OnError = function (status,responseText,context) {console.log("AjaxObject error:".concat(responseText));};
AjaxObject.prototype.ReadyStateChange = function () { 
    if (this.XmlHttp.readyState == 1) {
        this.OnLoading(this.mContext);
    }
    else if (this.XmlHttp.readyState == 2) {
        this.OnLoaded(this.mContext);
    }
    else if (this.XmlHttp.readyState == 3) {
        this.OnInteractive(this.mContext);
    }
    else if (this.XmlHttp.readyState == 4) { 
        if (this.XmlHttp.status == HttpStatusOk){          
            this.OnComplete(this.XmlHttp.responseText,this.mContext);       
        }            
        else{
            var status = this.XmlHttp.status, res = this.XmlHttp.responseText;           
            this.OnError(status,res,this.mContext);
        }           
    }
};
/**
*Api Info Struct
*auth:lww
*version:1.0
*create date:2021-02-01
*/
function ApiInfo(refContext, path, requestData, selfHeader){   
    this.context = refContext;
    this.url = path;
    this.header = selfHeader;
    this.data = requestData;
}
//init async instance factory, support:IE6-11 And All
function asyncRequest(method, contentType){
    var httpMethod = method;
    var httpContentType = contentType;
    function HttpRequest(refContext,path,requestData,selfHeader){ 
        this.context = refContext;
        this.url = path;
        this.data = requestData;
        this.header = selfHeader;       
        this.successHandler = function(responseText,context){console.log("asyncRequest success:".concat(responseText));};
        this.errorHandler = function(status,responseText,context){console.log("asyncRequest error:".concat(responseText));};      
    }
    HttpRequest.prototype.success = function(successCallbak){
        this.successHandler = successCallbak;
        return this;
    };
    HttpRequest.prototype.then = function(errorCallbak){   
        if(typeof(errorCallbak) === 'function'){
            this.errorHandler = errorCallbak;
        }  
        this.ajaxObject = new AjaxObject(this.context);   
        this.ajaxObject.selfHeader = this.header;        
        this.ajaxObject.OnComplete = this.successHandler;    
        this.ajaxObject.OnError = this.errorHandler;        
        this.ajaxObject.DoRequest(httpMethod,this.url,this.data,httpContentType);
        return this;
    }; 
    return function(apiInfo){
        return new HttpRequest(apiInfo.context,apiInfo.url,apiInfo.data,apiInfo.header);
    };   
}
//init request instance, support:IE6-11 And Other
var httpGet = asyncRequest(HttpGetMethod,DefaultContentType);
var httpPost = asyncRequest(HttpPostMethod,JSONContentType);
var httpFormPost = asyncRequest(HttpPostMethod,DefaultContentType);
var httpPut = asyncRequest(HttpPutMethod,JSONContentType);
var httpDelete = asyncRequest(HttpDeleteMethod,JSONContentType);

markscript("request");
//end request.js